package com.bykj.property.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @author yiming
* @description: TODO
* @date 2019-06-17
*/

public class ExcelUtils {
private ExcelUtils() {}

private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
throw new  RuntimeException(e);
}
}

private static<T> void defaultExport(List<T> dataList, Class<?> clz, String fileName, HttpServletResponse response, ExportParams exportParams) {
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, clz, dataList);
        if (workbook != null) {
        downLoadExcel(fileName, response, workbook);
        }
        }

        public static<T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz, String fileName, boolean isCreateHeader, HttpServletResponse response) {
                ExportParams exportParams = new ExportParams(title, sheetName);
                exportParams.setCreateHeadRows(isCreateHeader);
                defaultExport(dataList, clz, fileName, response, exportParams);
                }

                public static<T> void exportExcel(List<T> dataList, String title, String sheetName, Class<?> clz, String fileName, HttpServletResponse response) {
                        defaultExport(dataList, clz, fileName, response, new ExportParams(title, sheetName));
                        }

                        private static void defaultExport(List<Map<String, Object>> dataList, String fileName, HttpServletResponse response) {
                        Workbook workbook = ExcelExportUtil.exportExcel(dataList, ExcelType.HSSF);
                        if (workbook != null) {
                        downLoadExcel(fileName, response, workbook);
                        }
                        }

                        public static void exportExcel(List<Map<String, Object>> dataList, String fileName, HttpServletResponse response) {
                        defaultExport(dataList, fileName, response);
                        }

                        public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> clz) {
                                    if (StringUtils.isBlank(filePath)) {
                                    return null;
                                    }

                                    ImportParams params = new ImportParams();
                                    params.setTitleRows(titleRows);
                                    params.setHeadRows(headerRows);

                                    try {
                                    return ExcelImportUtil.importExcel(new File(filePath), clz, params);
                                    } catch (Exception e) {
                                    throw new RuntimeException(e.getMessage());
                                    }
                                    }

                                    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> clz) {
                                                if (file == null) {
                                                return null;
                                                }

                                                ImportParams params = new ImportParams();
                                                params.setTitleRows(titleRows);
                                                params.setHeadRows(headerRows);

                                                try {
                                                return ExcelImportUtil.importExcel(file.getInputStream(), clz, params);
                                                } catch (Exception e) {
                                                throw new RuntimeException(e);
                                                }
                                                }



                                }
